package com.shishuo.cms.action.manage;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.Config;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.ConfigService;

@Controller
@RequestMapping("/admin/config")
public class ConfigAction extends AdminBaseAction
{

	@Autowired
	private ConfigService configService;

	@AuthPassport
	@RequestMapping(value = "/base.htm", method = RequestMethod.GET)
	public String base(
						ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		List<Config> listConfig = configService.getAllList();
		for (int i = 0; i < listConfig.size(); i++)
		{
			if (listConfig.get(i).getCmsKey().equals("SYS_SITENAME"))
			{
				request.setAttribute("NAME", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("SYS_THEME"))
			{
				request.setAttribute("THEME", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("SYS_SITEDESC"))
			{
				request.setAttribute("SITEDESC", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("IS_CACHE_HTML"))
			{
				request.setAttribute("CACHE", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("SYS_KEYWORDS"))
			{
				request.setAttribute("SYS_KEYWORDS", listConfig.get(i).getCmsValue());
			}

			if (listConfig.get(i).getCmsKey().equals("SYS_SUB_GOLD"))
			{
				request.setAttribute("SYS_SUB_GOLD", listConfig.get(i).getCmsValue());
			}

			if (listConfig.get(i).getCmsKey().equals("referPercent"))
			{
				request.setAttribute("referPercent", listConfig.get(i).getCmsValue());
			}

			if (listConfig.get(i).getCmsKey().equals("pointParent"))
			{
				request.setAttribute("pointParent", listConfig.get(i).getCmsValue());
			}

			if (listConfig.get(i).getCmsKey().equals("maxLevel"))
			{
				request.setAttribute("maxLevel", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("goldToMoneyPercent"))
			{
				request.setAttribute("goldToMoneyPercent", listConfig.get(i).getCmsValue());
			}
			if (listConfig.get(i).getCmsKey().equals("subPointParent"))
			{
				request.setAttribute("subPointParent", listConfig.get(i).getCmsValue());
			}
		}
		modelMap.put("listConfig", listConfig);
		//获取网站模板文件夹
		List<String> templeteList = getAllTemplete();
		request.setAttribute("templeteList", templeteList);
		return "system/config/base";
	}

	/**
	 * 
	 * @since 2015年3月12日 下午5:50:53
	 * @return
	 */
	private List<String> getAllTemplete()
	{
		List<String> templateList = new ArrayList<String>();
		String templatePath = /*request.getServletContext().getRealPath("/") */getRoot()+ "/template/";
		File dir = new File(templatePath);
		File[] files = dir.listFiles();
		for (File file : files)
		{
			if (file.isDirectory() && !file.getName().equals("admin"))
			{
				templateList.add(file.getName());
			}
			logger.debug(file.getName());
		}
		return templateList;
	}
	public static String getRoot() {
		String rootKey = "shishuo.cms.root";
		String cmsRoot = System.getProperty(rootKey);
		Enumeration<?> enu = System.getProperties().propertyNames();
		while (enu.hasMoreElements()) {
			Object key = enu.nextElement();
			if (key.toString().startsWith(rootKey)) {
				cmsRoot = System.getProperty(key.toString());
				break;
			}
		}
		return cmsRoot;
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/basic.json", method = RequestMethod.POST)
	public JsonVo<String> updateBaseConfig(@RequestParam(value = "siteDesc") String siteDesc,
											@RequestParam(value = "siteName") String siteName,
											@RequestParam(value = "Keywords") String Keywords,
											@RequestParam(value = "template") String template,
											@RequestParam(value = "cache") String cache,
											@RequestParam(value = "SYS_SUB_GOLD") double SYS_SUB_GOLD,
											@RequestParam(value = "referPercent") double referPercent,
											@RequestParam(value = "pointParent") double pointParent,
											@RequestParam(value = "maxLevel") int maxLevel,
											@RequestParam(value = "goldToMoneyPercent") double goldToMoneyPercent,
											@RequestParam(value = "subPointParent") double subPointParent,
											@RequestParam(value = "inputMoneyCenterPercent") double inputMoneyCenterPercent,
											@RequestParam(value = "outputMoneyCenterPercent") double outputMoneyCenterPercent) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		if (siteName.equals(""))
		{
			json.getErrors().put("siteName", "站点名称不能为空");
		}
		if (siteDesc.equals(""))
		{
			json.getErrors().put("siteDesc", "站点描述不能为空");
		}
		if (Keywords.equals(""))
		{
			json.getErrors().put("Keywords", "站点关键词不能为空");
		}
		if (template.equals(""))
		{
			json.getErrors().put("template", "模板不能为空");
		}

		// 检测校验结果
		if (validateJsonIsError(json))
			return json;
		List<Config> allConfigs = new ArrayList<Config>();
		Config entity = new Config();
		entity.setCmsKey("SYS_SITENAME");
		entity.setCmsValue(siteName);
		allConfigs.add(entity);
		entity = new Config();
		entity.setCmsKey("SYS_THEME");
		entity.setCmsValue(template);
		allConfigs.add(entity);
		entity = new Config();
		entity.setCmsKey("SYS_SITEDESC");
		entity.setCmsValue(siteDesc);
		allConfigs.add(entity);
		entity = new Config();
		entity.setCmsKey("SYS_KEYWORDS");
		entity.setCmsValue(Keywords);
		allConfigs.add(entity);
		entity = new Config();
		entity.setCmsKey("IS_CACHE_HTML");
		entity.setCmsValue(cache);
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("SYS_SUB_GOLD");
		entity.setCmsValue(Double.toString(SYS_SUB_GOLD));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("referPercent");
		entity.setCmsValue(Double.toString(referPercent));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("pointParent");
		entity.setCmsValue(Double.toString(pointParent));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("maxLevel");
		entity.setCmsValue(Integer.toString(maxLevel));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("goldToMoneyPercent");
		entity.setCmsValue(Double.toString(goldToMoneyPercent));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("subPointParent");
		entity.setCmsValue(Double.toString(subPointParent));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("inputMoneyCenterPercent");
		entity.setCmsValue(Double.toString(inputMoneyCenterPercent));
		allConfigs.add(entity);

		entity = new Config();
		entity.setCmsKey("outputMoneyCenterPercent");
		entity.setCmsValue(Double.toString(outputMoneyCenterPercent));
		allConfigs.add(entity);
		configService.updateAll(allConfigs);
		json.setResult(true);
		return json;
	}

}
